package com.baitiaojun.video.handler.canal.video;

import com.baitiaojun.api.domain.user.UserInfo;
import com.baitiaojun.api.domain.video.Video;
import com.baitiaojun.api.util.AESUtil;
import com.baitiaojun.video.feign.ElasticsearchVideoFeign;
import com.baitiaojun.video.feign.UserFeign;
import com.google.common.collect.Sets;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.List;

/**
 * @类名 VideoSyncEsHandler
 * @作者 白条君
 * @创建日期 2023/1/24 23:36
 * @版本 1.0
 */
@Component
@CanalTable("t_video")
public class VideoSyncEsHandler implements EntryHandler<Video> {

    @Autowired
    private ElasticsearchVideoFeign elasticsearchVideoFeign;

    @Autowired
    private UserFeign userFeign;

    @SneakyThrows
    @Override
    public void insert(Video video) {
        AESUtil aesUtil = AESUtil.lazyGetAesUtil();
        video.setUrl(aesUtil.encrypt(video.getUrl()));
        video.setThumbnail(aesUtil.encrypt(video.getThumbnail()));
        //获取用户的昵称
        List<UserInfo> userInfos = userFeign.batchGetUserInfoByUserIdsSet(Sets.newHashSet(video.getUserId()));
        video.setCreator(userInfos.get(0).getNick());
        video.setUserId(null);
        elasticsearchVideoFeign.saveVideo(video);
    }

    @Override
    public void delete(Video video) {
        elasticsearchVideoFeign.removeVideo(video.getId());
    }
}
